package com.example.frn.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.frn.dto.BannerInsertDto;
import com.example.frn.dto.BannerUpdateDto;
import com.example.frn.dto.PageDTO;
import com.example.frn.entity.Banner;
import com.example.frn.service.BannerService;
import com.example.frn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/v1/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @PostMapping("save")
    public R save(@RequestBody BannerInsertDto dto){
        Banner banner = BeanUtil.copyProperties(dto, Banner.class);
        if(bannerService.save(banner))
            return R.ok(true);
        else return R.ok(false);
    }
    @PostMapping("update")
    public R update(@RequestBody BannerUpdateDto dto){
        Banner banner = BeanUtil.copyProperties(dto, Banner.class);
        if(bannerService.updateById(banner))
            return R.ok(true);
        else return R.ok(false);
    }
    @PostMapping("remove/{cid}")
    public R remove(@PathVariable("cid") Integer cid){
        if(bannerService.removeById(cid))
            return R.ok(true);
        else return R.ok(false);
    }
    @GetMapping("detail/{cid}")
    public R detail(@PathVariable("cid") Integer cid){
        Banner banner = bannerService.getById(cid);
        return R.ok(banner);
    }
    @PostMapping("page")
    public R page(@RequestBody PageDTO dto){
        return R.ok(bannerService.page(dto.getPageNum(),dto.getPageSize()));
    }
}
